package com.suep.data_analysis.controller;

import com.suep.data_analysis.dto.DriveSetDto;
import com.suep.data_analysis.dto.DrivePropertyDto;
import com.suep.data_analysis.service.DriveSearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("api/drive-sets")
public class DriveSearchController {
    private final DriveSearchService driveSearchService;  // 修正变量名，与服务类名一致

    @Operation(
            summary = "获取驱动盘套装列表",
            description = "查询所有驱动盘套装的基本信息（不含属性详情）",
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取套装列表",
                            content = @Content(mediaType = "application/json",
                                    schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = DriveSetDto.class)))
            }
    )
    @GetMapping
    public ResponseEntity<List<DriveSetDto>> getAllDriveSets() {
        List<DriveSetDto> driveSets = driveSearchService.getAllDriveSets();
        return ResponseEntity.ok(driveSets);
    }

    @Operation(
            summary = "根据ID查询套装详情",
            description = "查询指定套装的完整信息（含属性列表）",
            parameters = {
                    @Parameter(name = "setId", description = "套装ID", required = true,
                            examples = @ExampleObject(value = "SET001", description = "量子重构套装"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取套装详情",
                            content = @Content(mediaType = "application/json",
                                    schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = DriveSetDto.class))),
                    @ApiResponse(responseCode = "404", description = "套装不存在", content = @Content)
            }
    )
    @GetMapping("/{setId}")
    public ResponseEntity<DriveSetDto> getDriveSetById(@PathVariable String setId) {
        DriveSetDto driveSet = driveSearchService.getDriveSetById(setId);
        if (driveSet == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(driveSet);
    }

    @Operation(
            summary = "搜索驱动盘套装",
            description = "根据关键词搜索套装（支持名称、效果搜索）",
            parameters = {
                    @Parameter(name = "keyword", description = "搜索关键词", required = true,
                            examples = @ExampleObject(value = "暴击", description = "搜索包含'暴击'的套装"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取搜索结果",
                            content = @Content(mediaType = "application/json",
                                    schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = DriveSetDto.class)))
            }
    )
    @GetMapping("/search")
    public ResponseEntity<List<DriveSetDto>> searchDriveSets(@RequestParam String keyword) {
        List<DriveSetDto> result = driveSearchService.searchDriveSets(keyword);
        return ResponseEntity.ok(result);
    }

    @Operation(
            summary = "根据位置查询驱动盘属性",
            description = "查询指定位置的所有驱动盘属性类型",
            parameters = {
                    @Parameter(name = "position", description = "驱动盘位置（1-6）", required = true,
                            examples = @ExampleObject(value = "1", description = "查询1号位属性"))
            },
            responses = {
                    @ApiResponse(responseCode = "200", description = "成功获取属性列表",
                            content = @Content(mediaType = "application/json",
                                    schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = DrivePropertyDto.class))),
                    @ApiResponse(responseCode = "400", description = "位置参数无效（需1-6）", content = @Content)
            }
    )
    @GetMapping("/properties/position/{position}")
    public ResponseEntity<List<DrivePropertyDto>> getPropertiesByPosition(@PathVariable Integer position) {
        if (position < 1 || position > 6) {
            return ResponseEntity.badRequest().build();
        }
        List<DrivePropertyDto> properties = driveSearchService.getPropertiesByPosition(position);
        return ResponseEntity.ok(properties);
    }
}